import rclpy
from status_interfaces.msg import SystemStatus
from rclpy.node import Node
import psutil
import platform

class SysStatusPub(Node):
    def __init__(self,node_name):
        super().__init__(node_name=node_name)
        self.status_publisher = self.create_publisher(SystemStatus,node_name,10)  #发布者
        self.timer = self.create_timer(1.0,self.time_back)

    def time_back(self):
        cpu_percent = psutil.cpu_percent()  #获取cpu使用率
        memory_info = psutil.virtual_memory()  # 获取系统内存
        net_io_counters = psutil.net_io_counters() # 获取网络输入输出

        msg = SystemStatus()
        msg.stamp = self.get_clock().now().to_msg()
        msg.host_name = platform.node()
        msg.cpu_percent = cpu_percent
        msg.memory_percent = memory_info.percent
        msg.memory_total = memory_info.total / 1024 / 1024
        msg.memory_available = memory_info.available / 1024 / 1024
        msg.net_sent = net_io_counters.bytes_sent / 1024 / 1024
        msg.net_recv = net_io_counters.bytes_recv / 1024 / 1024

        self.get_logger().info(f"发布了{str(msg)}")
        self.status_publisher.publish(msg)


def main():
    rclpy.init()
    node = SysStatusPub("sys_status_pub")
    rclpy.spin(node)
    rclpy.shutdown()